package cn.ztf.zshop.sys.controller;

import cn.ztf.zshop.sys.dto.*;
import cn.ztf.zshop.sys.po.SysUser;
import cn.ztf.zshop.sys.service.SysUserService;
import cn.ztf.zshop.sys.vo.LoginInfo;
import cn.ztf.zshop.sys.vo.SysUserVo;
import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.service.CaptchaService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/sys/user")
@RequiredArgsConstructor
public class SysUserController {
    private final SysUserService sysUserService;
    private final CaptchaService captchaService;

    /**
     * 系统用户的分页查询
     *
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<Page<SysUserVo>> page(Page<SysUser> page) {
        Page<SysUserVo> sysUserVoPage = sysUserService.listPage(page);
        return ResponseEntity.ok(sysUserVoPage);
    }

    /**
     * 根据id查询系统用户
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<SysUserVo> get(@PathVariable Long id) {
        SysUserVo sysUserVo = sysUserService.getSysUserById(id);
        return ResponseEntity.ok(sysUserVo);
    }


    /**
     * 添加系统用户
     *
     * @param sysUserDto
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> add(@Validated @RequestBody SysUserDto sysUserDto) {
        sysUserService.create(sysUserDto);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改系统用户信息
     *
     * @param sysUserDto
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> edit(@Validated(SysUserDto.Update.class) @RequestBody SysUserDto sysUserDto) {
        sysUserService.edit(sysUserDto);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据id删除系统用户
     * TODO 删除需额外判断
     *
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        sysUserService.delete(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改个人基础信息
     *
     * @param sysUserInfoDto
     * @return
     */
    @PutMapping("/updateInfo")
    public ResponseEntity<Void> editInfo(@Validated @RequestBody SysUserInfoDto sysUserInfoDto) {
        sysUserService.editInfo(sysUserInfoDto);
        return ResponseEntity.ok().build();
    }


    /**
     * 修改密码
     *
     * @param sysUserPasswordDto
     * @return
     */
    @PutMapping("/updatePassword")
    public ResponseEntity<Void> updatePassword(@Validated @RequestBody SysUserPasswordDto sysUserPasswordDto) {
        sysUserService.updatePassword(sysUserPasswordDto);
        return ResponseEntity.ok().build();
    }

    /**
     * 用户登录
     *
     * @param userLoginDto
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity<LoginInfo> userLogin(@Validated @RequestBody UserLoginDto userLoginDto) {
        LoginInfo loginInfo = sysUserService.userLogin(userLoginDto);
        return ResponseEntity.ok(loginInfo);
    }

    /**
     * 用户登出
     *
     * @return
     */
    @GetMapping("/logout")
    public ResponseEntity<Void> userLogout() {
        sysUserService.userLogout();
        return ResponseEntity.ok().build();
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public ResponseEntity<Void> userRegister(@Validated @RequestBody SysUserRegisterDto sysUserRegisterDto) {
        sysUserService.userRegister(sysUserRegisterDto);
        return ResponseEntity.ok().build();
    }

//    /**
//     * 根据用户名查询用户
//     * @param username
//     * @return
//     */
//    @GetMapping("/username/{username}")
//    public ResponseEntity<SysUserVo> getByUsername(@PathVariable String username){
//        SysUserVo sysUserVo = sysUserService.getByUsername(username);
//        return ResponseEntity.ok(sysUserVo);
//    }

//    /**
//     * 根据用户id查询用户权限
//     */
//    @GetMapping("/permission/{id}")
//    public ResponseEntity<List<String>> getPermissionByUserId(@PathVariable Long id){
//        List<String> permissionList = sysUserService.getPermissionByUserId(id);
//        return ResponseEntity.ok(permissionList);
//    }


}
